package com.woniuxy.joylifecenter.service.impl;

import com.woniuxy.common.entity.CenterRole;
import com.woniuxy.common.entity.CenterRoleExample;
import com.woniuxy.common.mapper.CenterRoleMapper;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    CenterRoleMapper centerRoleMapper;


    /**
     * @param centerRole
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/6
     * @Param [centerRole]
     * @Description 添加角色
     */
    @Override
    public int addRole(CenterRole centerRole) {
        if (queryRoleByName(centerRole).size() == 0) {
            return centerRoleMapper.insertSelective(centerRole);
        } else {
            throw new CenterException("角色已存在，请勿重复添加");
        }
    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterRole>
     * @Author ArmWatt
     * @Date 2020/5/6
     * @Param [centerRole]
     * @Description 添加角色验重
     **/
    @Override
    public List<CenterRole> queryRoleByName(CenterRole centerRole) {
        CenterRoleExample example = new CenterRoleExample();
        CenterRoleExample.Criteria criteria = example.createCriteria();
        criteria.andCenterRoleNameEqualTo(centerRole.getCenterRoleName());
        return centerRoleMapper.selectByExample(example);
    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterRole>
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [centerRole]
     * @Description 多条件模糊查询
     **/
    @Override
    public List<CenterRole> queryRoleByCondition(CenterRole centerRole) {
        CenterRoleExample example = new CenterRoleExample();
        CenterRoleExample.Criteria criteria = example.createCriteria();
        if (centerRole.getCenterRoleid() != null) {
            criteria.andCenterRoleidLike("%" + centerRole.getCenterRoleid() + "%");
        }
        if (centerRole.getCenterRoleName() != null) {
            criteria.andCenterRoleNameLike("%" + centerRole.getCenterRoleName() + "%");
        }
        return centerRoleMapper.selectByExample(example);

    }

    /**
     * @param centerRole
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [centerRole]
     * @Description 编辑角色
     */
    @Override
    public int editRole(CenterRole centerRole) {

        if (editVerify(centerRole) == 0) {
            return centerRoleMapper.updateByPrimaryKey(centerRole);
        } else {
            throw new CenterException("角色已存在");
        }

    }

    /**
     * @param centerRole
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [centerRole]
     * @Description 编辑角色去重
     */
    @Override
    public int editVerify(CenterRole centerRole) {
        CenterRoleExample example = new CenterRoleExample();
        CenterRoleExample.Criteria criteria = example.createCriteria();
        criteria.andCenterRoleidNotEqualTo(centerRole.getCenterRoleid());
        criteria.andCenterRoleNameEqualTo(centerRole.getCenterRoleName());
        return centerRoleMapper.selectByExample(example).size();
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [roleId]
     * @Description 删除单个角色
     **/
    @Override
    public int deleteRole(String roleId) {
        int count = centerRoleMapper.deleteByPrimaryKey(roleId);
        if (count != 1) {
            throw new CenterException("删除失败，请勿重复删除");
        }
        return count;
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [roleIds]
     * @Description 批量删除角色
     **/
    @Override
    public int batchDeleteRole(List<String> roleIds) {
        CenterRoleExample example = new CenterRoleExample();
        CenterRoleExample.Criteria criteria = example.createCriteria();
        criteria.andCenterRoleidIn(roleIds);
        int count = centerRoleMapper.deleteByExample(example);
        if (count == 0) {
            throw new CenterException("批量删除失败，请勿重复删除");
        }
        return count;
    }


}

